package stack;


import java.util.Stack;

                                               //逆序打印链表元素
public class Practice1 {
    static class Node {
        public String val;
        public Node next;
        public Node(String val){
            this.val = val;
            next = null;
        }
    }
    public static Node build(){
        Node a = new Node("a");
        Node b = new Node("b");
        Node c = new Node("c");
        Node d = new Node("d");
        a.next = b;
        b.next = c;
        c.next = d;
        d.next = null;
        return a;
    }
    public static void reversePrint(Node head){
        //1.使用递归的方法
//        if (head == null){
//            return;
//        }
//        reversePrint(head.next);
//        System.out.println(head.val);
        //2.使用栈的写法
        if (head == null){
            return;
        }
        //先将链表依次入栈
        Stack<Node> stack = new Stack<>();
        for (Node cur = head; cur != null; cur = cur.next){
            stack.push(cur);
        }
        //再依次出栈
        while (!stack.isEmpty()){
            System.out.println(stack.pop().val);
        }
    }


    public static void main(String[] args) {
        Node head = build();
        reversePrint(head);
    }
}
